package com.coai.rtc.controller

import com.coai.common.dto.ApiResponse
import com.coai.rtc.dto.*
import com.coai.rtc.service.CallService
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/calls")
class CallController(
    private val callService: CallService
) {
    
    /**
     * 发起通话
     */
    @PostMapping("/initiate")
    fun initiateCall(
        @RequestBody request: InitiateCallRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<CallDto> {
        val call = callService.initiateCall(request, userId)
        return ApiResponse.success(call, "通话已发起")
    }
    
    /**
     * 接受通话
     */
    @PostMapping("/{callId}/accept")
    fun acceptCall(
        @PathVariable callId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<CallDto> {
        val call = callService.acceptCall(callId, userId)
        return ApiResponse.success(call, "已加入通话")
    }
    
    /**
     * 拒绝通话
     */
    @PostMapping("/{callId}/reject")
    fun rejectCall(
        @PathVariable callId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<CallDto> {
        val call = callService.rejectCall(callId, userId)
        return ApiResponse.success(call, "已拒绝通话")
    }
    
    /**
     * 结束通话
     */
    @PostMapping("/{callId}/end")
    fun endCall(
        @PathVariable callId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<CallDto> {
        val call = callService.endCall(callId, userId)
        return ApiResponse.success(call, "通话已结束")
    }
    
    /**
     * 离开通话
     */
    @PostMapping("/{callId}/leave")
    fun leaveCall(
        @PathVariable callId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<CallDto> {
        val call = callService.leaveCall(callId, userId)
        return ApiResponse.success(call, "已离开通话")
    }
    
    /**
     * 更新媒体状态
     */
    @PutMapping("/{callId}/media-state")
    fun updateMediaState(
        @PathVariable callId: String,
        @RequestBody request: UpdateMediaStateRequest,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<ParticipantDto> {
        val participant = callService.updateMediaState(callId, userId, request)
        return ApiResponse.success(participant)
    }
    
    /**
     * 获取通话详情
     */
    @GetMapping("/{callId}")
    fun getCall(
        @PathVariable callId: String,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<CallDto> {
        val call = callService.getCall(callId)
        return ApiResponse.success(call)
    }
    
    /**
     * 获取通话历史
     */
    @GetMapping("/history")
    fun getCallHistory(
        @RequestParam(defaultValue = "0") page: Int,
        @RequestParam(defaultValue = "20") size: Int,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<CallListResponse> {
        val history = callService.getUserCallHistory(userId, page, size)
        return ApiResponse.success(history)
    }
    
    /**
     * 获取通话统计
     */
    @GetMapping("/statistics")
    fun getStatistics(
        @RequestParam(defaultValue = "30") days: Int,
        @AuthenticationPrincipal userId: String
    ): ApiResponse<CallStatistics> {
        val stats = callService.getCallStatistics(userId, days)
        return ApiResponse.success(stats)
    }
}
